// pages/login/login.js
var Config = require('../../Config');
var Http = require('../../utils/http');
var Utils = require('../../utils/util');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ajaxStatus: '',
    ajaxInfo: '',
    loginLoading: false,
    errorInfo: '',
    courseSelect: [
      // { shop_id: '111', shop_name: '电话' }
    ],
    loginForm: {
      mobile: '',
      psw: '',
      shop_id: '',
      shop_name: ''
    }
  },
  phoneInput(e) {
    let self = this;
    let phoneNumber = e.detail.value;
    if (phoneNumber.length === 11) {
      console.log(phoneNumber);
      wx.showLoading({
        title: '获取场馆列表中...',
      })
      Http.post('/GetShopList', { mobile: phoneNumber}, (res) => {
        wx.hideLoading();
        if (res.flag) {
          self.setData({
            'courseSelect': res.data,
            'ajaxStatus': '',
            'ajaxInfo': ''
          })
          // 如果只有一个场馆，默认选中
          if (res.data.length === 1) {
            self.setData({
              'loginForm.shop_id': res.data[0].id,
              'loginForm.shop_name': res.data[0].shop_name
            })
          }
        } else {
          wx.showToast({
            title: '没有获取到场馆数据',
            icon: 'none'
          })
          self.setData({
            'courseSelect': [],
            'loginForm.shop_id': '',
            'loginForm.shop_name': '',
            'ajaxStatus': 'warning',
            'ajaxInfo': '当前手机号码不存在于任何场馆'
          })
        }
      }, () => {
        wx.hideLoading();
      });
    } else {
      self.setData({
        'courseSelect': [],
        'loginForm.shop_id': '',
        'loginForm.shop_name': '',
      })
    }
  },
  phoneBlur(e) {
    this.setData({
      "loginForm.mobile": e.detail.value
    })
  },
  pswBlur(e) {
    this.setData({
      "loginForm.psw": e.detail.value
    });
  },
  courseChanged(e) {
    console.log(e);
    let index = e.detail.value / 1;
    this.setData({
      'loginForm.shop_id': this.data.courseSelect[index].id,
      'loginForm.shop_name': this.data.courseSelect[index].shop_name
    })
  },
  submitLogin() {
    let self = this;
    let data = this.data.loginForm;
    if (data.mobile === '' || !Utils.Vertify.isMobile(data.mobile)) {
      self.setData({
        "ajaxStatus": 'error',
        "ajaxInfo": '请输入正确的手机号码'
      });
      return false;
    }
    if (data.shop_id === '') {
      self.setData({
        "ajaxStatus": 'error',
        "ajaxInfo": '请选择一个场馆'
      });
      return false;
    }
    if (data.psw === '') {
      self.setData({
        "ajaxStatus": 'error',
        "ajaxInfo": '请输入密码'
      });
      return false;
    }
    this.setData({
      "loginLoading": true
    });
    Http.post('/login', data, function(res){
      self.setData({
        "loginLoading": false
      });
      if (res.flag) {
        self.setData({
          "ajaxStatus": 'success',
          "ajaxInfo": '登录成功'
        });
        wx.setStorage({
          key: 'teacherInfo',
          data: Utils.extend(true, { shop_name: data.shop_name }, res.data),
        })
        wx.switchTab({
          url: '/pages/index/index?title=123',
        });
      } else {
        self.setData({
          "ajaxStatus": 'error',
          "ajaxInfo": res.msg
        });
      }
    }, function() {
      this.setData({
        "loginLoading": false
      });
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 检查登录状态
    wx.getStorage({
      key: 'teacherInfo',
      success: function (res) {
        wx.switchTab({
          url: '/pages/index/index',
        })
      },
      fail() {
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  }
})